<?php

namespace App\Model\Service;

use App\Model\CustomerModel;
use Trois\Lib\Help;

class CustomerService extends BaseService
{
    public function list(): array
    {
//        return [];

        // 初始条件
        $where = [
            'customer.group_id' => $this->loginUser['group_id'],
            'customer.biz_id' => $this->loginUser['biz_id'],
            'customer.status' => [99, '!=']
        ];

        // 事业部
        if ($this->postParams['biz_id']) {
            $where['customer.biz_id'] = $this->postParams['biz_id'];
        }

        // 品牌
        if ($this->postParams['brand']) {
            $postBrand = explode(",", $this->postParams['brand']);
            $where['customer.bra_id'] = count($postBrand) == 1 ? $postBrand[0] : [$postBrand, 'in'];
        }

        // 城市
        if ($this->postParams['city']) {
            $postCity = explode(",", $this->postParams['city']);
            $where['customer.city'] = count($postCity) == 1 ? $postCity[0] : [$postCity, 'in'];
        }

        // 门店
        if ($this->postParams['shop']) {
            $postShop = explode(",", $this->postParams['shop']);
            $where['customer.dept_id'] = count($postShop) == 1 ? $postShop[0] : [$postShop, 'in'];
        }

        // 添加部门
        if ($this->postParams['add_dept_id']) {
            $postAddDeptId = explode(",", $this->postParams['add_dept_id']);
            $where['customer.id_add_dept'] = count($postAddDeptId) == 1 ? $postAddDeptId[0] : [$postAddDeptId, 'in'];
        }

        // 添加人
        if ($this->postParams['add_user_id']) {
            $postAddUserId = explode(",", $this->postParams['add_user_id']);
            $where['customer.id_add_op'] = count($postAddUserId) == 1 ? $postAddUserId[0] : [$postAddUserId, 'in'];
        }

        // 运营部门
        if ($this->postParams['operator_user_dept']) {
            $postAddDeptId = explode(",", $this->postParams['operator_user_dept']);
            $where['customer.operator_user_dept'] = count($postAddDeptId) == 1 ? $postAddDeptId[0] : [$postAddDeptId, 'in'];
        }

        // 运营人
        if ($this->postParams['operator_user_id']) {
            $postAddUserId = explode(",", $this->postParams['operator_user_id']);
            $where['customer.operator_user_id'] = count($postAddUserId) == 1 ? $postAddUserId[0] : [$postAddUserId, 'in'];
        }

        // 性别 (0为未知，这里需要用!=='')
        if ($this->postParams['sex'] !== '') {
            $where['customer.cus_sex'] = $this->postParams['sex'];
        }

        // 类型
        if ($this->postParams['cus_type']) {
            $where['customer.cus_type'] = $this->postParams['cus_type'];
        }

        // 删除状态
        if ($this->postParams['status'] === 99) {
            $where['customer.status'] = 99;
        }

        // 客删状态
        if ($this->postParams['is_del_follow_user'] === 1) {
            $where['customer.is_del_follow_user'] = 1;
        }

        // 开口
        if ($this->postParams['kaikou_nums'] !== '') {

            if ($this->postParams['kaikou_nums'] === 0) {
                $where['customer.is_kaikou'] = 0;
            } else if ($this->postParams['kaikou_nums'] === 1) {
                $where['customer.is_kaikou'] = 1;
            } else if ($this->postParams['kaikou_nums'] === 3) {
                $where['customer.kaikou_nums'] = [3, '>'];
            }

        }

        $date_start = strtotime($this->postParams['date_start']);
        $date_end = strtotime($this->postParams['date_end']);
        $date_end2 = strtotime($this->postParams['date_end'] . ' +1 day');

        switch ($this->postParams['date_type']) {
            case 'first_deposit':
                $where['customer.first_deposit'] = [[$date_start, $date_end2], 'BETWEEN'];
                break;
            case 'first_shop':
                $where['customer.first_shop'] = [[$date_start, $date_end2], 'BETWEEN'];
                break;
            case 'wechat_join':
            default:
                if ($date_start == $date_end) {
                    $where['customer.wechat_join_day'] = $date_start;
                } else {
                    $where['customer.wechat_join_day'] = [[$date_start, $date_end], 'BETWEEN'];
                }
                break;
        }


        if ($this->postParams['keyword']) {
            switch ($this->postParams['keyword_type']) {
                case '3':
                    $where['customer.cus_mobile'] = $this->postParams['keyword'];
                    break;
                case '2':
                    $where['customer.cus_name'] = ['%' . $this->postParams['keyword'] . '%', 'like'];
                    break;
                case '1':
                default:
                    $where['customer.cus_id'] = $this->postParams['keyword'];
                    break;
            }
        }


        $cart = Help::modelInvoke(CustomerModel::class, function (CustomerModel $model) use ($where) {
            $list = $model
                ->field('
                customer.cus_name
                ,customer.cus_id
                ,customer.cus_sex
                ,customer.cus_img
                ,customer.cus_type
                ,customer.status
                ,customer.id_add_op
                ,customer.id_add_dept
                ,customer.name_add_op
                ,customer.name_add_dept
                ,customer.addtime
                ,customer.remarks
                ,customer.province
                ,customer.province_name
                ,customer.city
                ,customer.city_name
                ,customer.area
                ,customer.area_name
                ,customer.wechat_join_day
                ,customer.id_join_type
                ,customer.operator_user_id
                ,customer.operator_user_dept
                ,customer.supplie_id
                ,customer.inherit_id
                ,customer.is_kaikou
                ,customer.is_del_follow_user
                ,customer.kaikou_nums
                ,customer.first_shop
                ,customer.first_deposit
                ,customer.biz_id
                ,business.biz_name
                ,shop.dept_name as shop_name
                ,brand.bra_name
                ,operator_user.user_name as operator_user_name
                ,operator_dept.dept_name as operator_dept_name
                ,account.last_shop
                ,account.id_ascr_op
                ,account.id_ascr_dept
                ,account.name_ascr_op
                ,account.name_ascr_dept
                ,supplier.supplie_id as supplie_name
                ,customer.source_id
                ,source.cname as source_name
                ')
                ->alias('customer')
                ->join('by_crm_dept as shop', 'shop.dept_id = customer.dept_id', 'left')
                ->join('by_crm_business as business', 'business.biz_id = customer.biz_id', 'left')
                ->join('by_common_customer_account as account', 'account.cus_id = customer.cus_id', 'left')
                ->join('by_common_customer_source as source', 'source.id = customer.source_id', 'left')
                ->join('by_crm_add_supplier_model as supplier', 'supplier.id = customer.supplie_id', 'left')
                ->join('by_crm_brand as brand', 'brand.bra_id = customer.bra_id', 'left')
                ->join('by_crm_user as operator_user', 'operator_user.user_id = customer.operator_user_id', 'left')
                ->join('by_crm_dept as operator_dept', 'operator_dept.dept_id = customer.operator_user_dept', 'left')
                ->limit($this->postParams['limit'] * ($this->postParams['page'] - 1), $this->postParams['limit'])
                ->order('customer.cus_id', 'desc')
                ->withTotalCount()
                ->all($where)
                ->toArray(false, false);

            $total = $model->lastQueryResult()->getTotalCount();

            return [
                'total' => $total,                                                  // 总数据数
                'list' => $list,                                                    // 当前数据
                'pages' => ceil($total / $this->postParams['limit']),              // 总页数
                'page' => $this->postParams['page'],                                    // 当前所在页
                'limit' => $this->postParams['limit'],                                  // 每页数据数
            ];


        }, false);

//        dd(Help::getLastSql());

        return $cart ?: [
            'total' => 0,
            'list' => [],
            'pages' => 1,
            'page' => $this->postParams['page'],
            'limit' => $this->postParams['limit'],
        ];
    }

    public function view(): array
    {
//        return [];

        // 初始条件
        $where = [
            'customer.group_id' => $this->loginUser['group_id'],
            'customer.biz_id' => $this->loginUser['biz_id'],
            'customer.cus_id' => $this->postParams['cus_id'],
        ];

        $res = Help::modelInvoke(CustomerModel::class, function (CustomerModel $model) use ($where) {
            return $model
                ->field('
                customer.cus_name
                ,customer.cus_id
                ,customer.cus_sex
                ,customer.cus_img
                ,customer.cus_type
                ,customer.status
                ,customer.id_add_op
                ,customer.id_add_dept
                ,customer.name_add_op
                ,customer.name_add_dept
                ,customer.addtime
                ,customer.remarks
                ,customer.province
                ,customer.province_name
                ,customer.city
                ,customer.city_name
                ,customer.area
                ,customer.area_name
                ,customer.wechat_join_day
                ,customer.id_join_type
                ,customer.operator_user_id
                ,customer.operator_user_dept
                ,customer.supplie_id
                ,customer.inherit_id
                ,customer.is_kaikou
                ,customer.is_del_follow_user
                ,customer.kaikou_nums
                ,customer.first_shop
                ,customer.first_deposit
                ,customer.biz_id
                ,business.biz_name
                ,shop.dept_name as shop_name
                ,brand.bra_name
                ,operator_user.user_name as operator_user_name
                ,operator_dept.dept_name as operator_dept_name
                ,account.last_shop
                ,account.id_ascr_op
                ,account.id_ascr_dept
                ,account.name_ascr_op
                ,account.name_ascr_dept
                ,supplier.supplie_id as supplie_name
                ,customer.source_id
                ,source.cname as source_name
                ')
                ->alias('customer')
                ->join('by_crm_dept as shop', 'shop.dept_id = customer.dept_id', 'left')
                ->join('by_crm_business as business', 'business.biz_id = customer.biz_id', 'left')
                ->join('by_common_customer_account as account', 'account.cus_id = customer.cus_id', 'left')
                ->join('by_common_customer_source as source', 'source.id = customer.source_id', 'left')
                ->join('by_crm_add_supplier_model as supplier', 'supplier.id = customer.supplie_id', 'left')
                ->join('by_crm_brand as brand', 'brand.bra_id = customer.bra_id', 'left')
                ->join('by_crm_user as operator_user', 'operator_user.user_id = customer.operator_user_id', 'left')
                ->join('by_crm_dept as operator_dept', 'operator_dept.dept_id = customer.operator_user_dept', 'left')
                ->where($where)
                ->get();
        });
        return $res;
    }
}